package com.cqh.recruit.controller.student;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqh.recruit.pojo.entity.Student;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IStudentService;
import com.cqh.recruit.utils.StudentThreadLocal;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 *
 *  前端控制器
 *
 *
 * @author dh
 * @since 2025-10-18
 * @version v1.0
 */
@RestController("studentStudentController")
@RequestMapping("/api/student/student")
@Tag(name = "学生信息表接口")
public class StudentController {


    @Autowired
    private IStudentService studentService;


    /**
     * 获取当前登录学生的信息
     * @return Result
     */
    @GetMapping("/findInfo")
    public Result findInfo(){
        Student student =studentService.findStudentInfoById();
        if (student == null) {
            return Result.buildFail(40001, "用户名或密码错误!");
        }
        return Result.buildSuccess(student);
    }

    /**
     * 学生修改个人基本信息
     * @param student
     * @return Result
     */
    @PostMapping("/updateStudentInfo")
    public Result updateStudentInfo(@RequestBody Student student){
        // 获取当前登录学生ID
        Long stuId = StudentThreadLocal.get().getId();

        // 只更新允许修改的字段
        Student updateStudent = new Student();
        updateStudent.setId(stuId);
        updateStudent.setName(student.getName());
        updateStudent.setPhone(student.getPhone());
        updateStudent.setEmail(student.getEmail());
        updateStudent.setGender(student.getGender());
        updateStudent.setAvatar(student.getAvatar());

        // 执行更新
        boolean success = studentService.updateById(updateStudent);

        if (success) {
            return Result.buildSuccess(updateStudent);
        } else {
            return Result.buildFail(500,"更新失败");
        }
    }

}

